﻿/* 2012/4/17 */
using System;
using System.IO;
using System.Collections.Generic;

namespace Cosmos.IO.FileSystemPath
{
    /// <summary>
    /// Provides path related operations.
    /// </summary>
    public sealed class FilePath
    {
        private static char[] _invalidFileNameChars;

        #region Extensions

        /// <summary>
        /// Extensions of static image file types.
        /// </summary>
        private static string[] _staticImageFileExtension = new string[] { ".jpg", ".jpeg", ".bmp", ".png" };

        /// <summary>
        /// Extensions of dynamic image file types.
        /// </summary>
        private static string[] _dynamicImageFileExtension = new string[] { ".gif" };

        private static string[] _executiveFileExtension = new string[] { ".exe", ".com" };

        #endregion

        static FilePath()
        {
            List<char> invalidCharList = new List<char>();
            invalidCharList.Add('\\');
            invalidCharList.Add('/');
            invalidCharList.Add(':');
            invalidCharList.Add('*');
            invalidCharList.Add('?');
            invalidCharList.AddRange(Path.GetInvalidFileNameChars());
            _invalidFileNameChars = invalidCharList.ToArray();
        }

        #region Extension Methods

        public static bool IsStaticImageExtension(string fileExtension)
        {
            if (fileExtension == null)
                return false;

            fileExtension = fileExtension.ToLower();
            foreach (string aExtension in _staticImageFileExtension)
            {
                if (aExtension.Equals(fileExtension))
                    return true;
            }
            return false;
        }

        public static bool IsDynamicImageExtension(string fileExtension)
        {
            if (fileExtension == null)
                return false;

            fileExtension = fileExtension.ToLower();
            foreach (string aExtension in _dynamicImageFileExtension)
            {
                if (aExtension.Equals(fileExtension))
                    return true;
            }
            return false;
        }

        public static bool IsImageExtension(string fileExtension)
        {
            return IsStaticImageExtension(fileExtension) || IsDynamicImageExtension(fileExtension);
        }

        #endregion

        #region Path Methods

        /// <summary>
        /// Returns a value indicating whether the specified file path is a static image file path.
        /// </summary>
        /// <param name="imagePath">FilePath of a file.</param>
        /// <returns>true if the file is a static image file; otherwise false.</returns>
        public static bool IsStaticImageFile(string filePath)
        {
            if (String.IsNullOrEmpty(filePath))
                return false;

            string fileExtension = Path.GetExtension(filePath);
            return IsStaticImageExtension(fileExtension);
        }

        /// <summary>
        /// Returns a value indicating whether the specified file path is a dynamic image file path.
        /// </summary>
        /// <param name="imagePath">FilePath of a file.</param>
        /// <returns>true if the file is a dynamic image file; otherwise false.</returns>
        public static bool IsDynamicImageFile(string filePath)
        {
            if (String.IsNullOrEmpty(filePath))
                return false;

            string fileExtension = Path.GetExtension(filePath);
            return IsDynamicImageExtension(fileExtension);
        }

        /// <summary>
        /// Returns a value indicating whether the specified file path is a image file path.
        /// </summary>
        /// <param name="imagePath">FilePath of a file.</param>
        /// <returns>true if the file is an image file; otherwise false.</returns>
        public static bool IsImageFile(string filePath)
        {
            if (String.IsNullOrEmpty(filePath))
                return false;

            return IsStaticImageFile((filePath)) || IsDynamicImageFile(filePath);
        }

        #endregion

        /// <summary>
        /// Gets an array containing the characters that are not allowed in file names.
        /// </summary>
        /// <returns>An array containing the characters that are not allowed in file names.</returns>
        public static char[] GetInvalidFileNameChars()
        {
            return _invalidFileNameChars;
        }
    }
}
